package sk.model;

import java.util.ArrayList;
import java.util.List;

public class Session {

	private String information;
	private int id;
	private List<Game> games;
	private List<Player> players;

	public Session(int id) {
		this.id = id;
		games = new ArrayList<Game>();
	}

	public String getInformation() {
		return information;
	}

	public Session(String string) {
		games = new ArrayList<Game>();
		players = new ArrayList<Player>();
		String[] tokens = string.split("!");
		id = Integer.parseInt(tokens[0]);
		int playerSize = Integer.parseInt(tokens[1]);
		for (int i = 2; i < 2 + playerSize; i++) {
			players.add(new Player(tokens[i]));
		}
		for (int i = playerSize + 2; i < tokens.length; i++) {
			games.add(new Game(tokens[i]));
		}
	}

	public boolean addGame(Game g) {
		if (g == null || g.getPlayer() == null || g.getOpponents() == null
			|| g.getSchneider() == null || g.getType() == null) {
			return false;
		} else if (g.getType() == GameType.SAUSPIEL && g.getTeamMate() == null) {
			return false;
		}
		return games.add(g);
	}

	public void setPlayers(List<Player> players) {
		this.players = players;
	}

	public List<Game> getGames() {
		return games;
	}

	public List<Player> getPlayers() {
		return players;
	}

	@Override
	public String toString() {
		String returnString = "" + id + "!";
		returnString += players.size() + "!";
		for (int i = 0; i < players.size(); i++) {
			returnString += players.get(i).toString() + "!";
		}
		for (int i = 0; i < games.size(); i++) {
			returnString += games.get(i).toString() + "!";
		}
		return returnString;
	}

	public int getID() {
		return id;
	}

	public int getResult(Player player) {
		int result = 0;
		for (Game game : games) {
			result += game.getResult(player);
		}
		return result;
	}

}
